package com.edais.actions;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.edais.service.PushService;
import com.edais.service.SecurityService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;

@Controller
@RequestMapping("/push")

public class PushAction  implements Serializable{

	private static final Log logger = LogFactory.getLog(PushAction.class);

	@Autowired
	private PushService pushService;
	
	@Autowired
	private SecurityService securityService;
	/**
	 * 消息列表
	 */
	@RequestMapping(value = "/getMessageList")
	public void getMessageList(HttpServletRequest request, HttpServletResponse response){
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String userId = paramsMap.get("userId");// 用户id
		String currentStr = paramsMap.get(Consts.Page.PAGE_CURRENT_NAME); // 当前页数
		String pageSizeStr = paramsMap.get(Consts.Page.PAGE_SIZE_NAME); // 页大小
		
		Integer current = 1;
		Integer pageSize = 10;
		
		if (StringUtils.isBlank(userId)) {
			logger.debug("push/getMessageList 传入用户id为空");
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
		
		try{
			if (currentStr != null && !("".equals(currentStr))) {
				current = Integer.parseInt(currentStr);
			}
			if (pageSizeStr != null && !("".equals(pageSizeStr))) {
				pageSize = Integer.parseInt(pageSizeStr);
			}
			
			if(current <= 0 || pageSize <= 0){
				logger.debug("push/getMessageList 传入当前页数/页大小为空");
				String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
				// 处理返回结果
				CommonUtil.responseJson(result, response);
				return;
			}
			
			String result = pushService.getMessageList(userId,current,pageSize);
//			String result = CommonUtil.setResultStringCn(
//				jsonMap,Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE, null);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		}catch(Exception ex){
			logger.debug("push/getMessageList 异常" + ex.getMessage());
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
		}
	}
	
	/**
	 * 消息详情
	 */
	@RequestMapping(value = "/getMessageDetail")
	public void getMessageDetail(HttpServletRequest request, HttpServletResponse response){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String messageId = paramsMap.get("messageId");// 消息id
		
		if (StringUtils.isBlank(messageId)) {
			logger.debug("push/getMessageDetail 传入消息id为空");
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
		
		try{
			String result = pushService.getMessageDetail(messageId);
//			String result = CommonUtil.setResultStringCn(
//				jsonMap,Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE, null);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			
		}catch(Exception ex){
			logger.debug("push/getMessageDetail 异常" + ex.getMessage());
			String result = CommonUtil.setResultStringCn(
				new HashMap<String, Object>(),Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.BUSINESSE_ERROR);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
		}
	}
	
//	/**
//	 * 设备注册接口
//	 * @param request
//	 * @param response
//	 */
//	@RequestMapping(value = "/addDevice")
//	public void addDevice(HttpServletRequest request, HttpServletResponse response){
//		// 解密请求参数
//		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
//		String userId = paramsMap.get("userId");// 用户id
//		String deviceNo = paramsMap.get("deviceNo");// 设备编号
//		String deviceType = paramsMap.get("deviceType");// 设备类型：1表示IOS，2表示android
//		
//		if (StringUtils.isBlank(userId) || StringUtils.isBlank(deviceNo) || StringUtils.isBlank(deviceType)) {
//			logger.debug("push/addDevice 传入参数为空");
//			// 处理返回结果
//			String result = CommonUtil.setResultStringCn(
//					new HashMap<String, Object>(), Consts.ERROR_CODE,
//					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.PARAMS_IS_NULL);
//			CommonUtil.responseJson(result, response);
//			return;
//		}
//		if (!("1".equals(deviceType) || "2".equals(deviceType))) {
//			logger.debug("push/addDevice 设备类型有误");
//			String result = CommonUtil.setResultStringCn(
//					new HashMap<String, Object>(),Consts.CHECK_CODE, 
//					Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.DEVICE_TYPE_ERROR);
//			// 处理返回结果
//			CommonUtil.responseJson(result, response);
//			return;
//		}
//		
//		try{
//			@SuppressWarnings("unchecked")
//			Map<String, String> jsonMap = new HashMap();
//			Map<String, String> map = pushService.addDevice(paramsMap);
//			jsonMap.put("rescode",(String) map.get("code"));
//			//jsonMap.put("resmsg", (String) map.get("resmsg"));
//			jsonMap.put("resmsg_cn", (String) map.get("resmsg"));
//			
//			//jsonMap.put("rescode", Consts.SUCCESS_CODE);
//			//jsonMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
//			//jsonMap.put("resmsg_cn", Message.CnDescriptionExpression.ADD_DEVICE_SUCCESS);
//			String result = JsonUtil.getJsonStringFromMap(jsonMap);
//			// 处理返回结果
//			CommonUtil.responseJson(result, response);
//			
//		}catch(Exception ex){
//			ex.printStackTrace();
//			logger.debug("push/addDevice 异常" + ex.getMessage());
//			String result = CommonUtil.setResultStringCn(
//					new HashMap<String, Object>(),Consts.ERROR_CODE,
//					null,Message.CnDescriptionExpression.ADD_DEVICE_FAIL
//					);
//			// 处理返回结果
//			CommonUtil.responseJson(result, response);
//			return;
//		}
//	}
	
	/**
	 * 设备注销接口
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/cancelDevice")
	public void cancelDevice(HttpServletRequest request, HttpServletResponse response){
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		String userId = paramsMap.get("userId");// 用户id
//		String deviceNo = paramsMap.get("deviceNo");// 设备编号
//		String deviceType = paramsMap.get("deviceType");// 设备类型：1表示IOS，2表示android
		
		String uuid = paramsMap.get("uuid");
		
		try{
			if (StringUtils.isBlank(userId)) {
				logger.debug("push/addDevice 传入参数为空");
				// 处理返回结果
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.ERROR_CODE,
						"",Message.CnDescriptionExpression.PARAMS_IS_NULL);
				CommonUtil.responseJson(result, response);
				return;
			}
			
			// uuid清除 -2.1.0版本新增
			if(StringUtils.isNotBlank(uuid) && StringUtils.isNotBlank(userId)){
				securityService.updExitTokenStr(paramsMap);
			}
			
			/*if (StringUtils.isBlank(userId) || StringUtils.isBlank(deviceNo) || StringUtils.isBlank(deviceType)) {
				logger.debug("push/addDevice 传入参数为空");
				// 处理返回结果
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), Consts.ERROR_CODE,
						Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.PARAMS_IS_NULL);
				CommonUtil.responseJson(result, response);
				return;
			}*/
			/*if (!("1".equals(deviceType) || "2".equals(deviceType))) {
				logger.debug("push/addDevice 设备类型有误");
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(),Consts.CHECK_CODE, 
						Consts.ErrorMsg.MSG00001,Message.CnDescriptionExpression.DEVICE_TYPE_ERROR);
				// 处理返回结果
				CommonUtil.responseJson(result, response);
				return;
			}*/
		

//			Map<String, String> jsonMap = new HashMap();
//			Map<String, String> map = pushService.cancelDevice(paramsMap);
//			jsonMap.put("rescode",(String) map.get("code"));
			//jsonMap.put("resmsg", (String) map.get("resmsg"));
//			jsonMap.put("resmsg_cn", (String) map.get("resmsg"));
//			pushService.cancelDevice(paramMap);
			String result = CommonUtil.setResultStringCn(new HashMap<String,Object>(),
					Consts.SUCCESS_CODE, Consts.SUCCESS_DESCRIBE,
					"");
			CommonUtil.responseJson(result, response);
				
		/*	String result = JsonUtil.getJsonStringFromMap(jsonMap);
			// 处理返回结果
			CommonUtil.responseJson(result, response);*/
			
		}catch(Exception ex){
			logger.debug("push/addDevice 异常" + ex.getMessage());
			String result = CommonUtil.setResultStringCn(
					new HashMap<String, Object>(),Consts.ERROR_CODE,
					null,Message.CnDescriptionExpression.CANCEL_DEVICE_FAIL);
			// 处理返回结果
			CommonUtil.responseJson(result, response);
			return;
		}
	}
	
	
}
